package cn.swenty.backstage.framework.database.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.swenty.backstage.framework.cache.core.eh.CacheClassify;
import cn.swenty.backstage.framework.cache.core.eh.EhcacheFactory;
import cn.swenty.backstage.framework.common.FormMap;
import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.database.annotation.TableSeg;
import cn.swenty.backstage.framework.database.mapper.BasicMapper;
import cn.swenty.backstage.framework.util.ClassUtil;

public class TableAction {
	
	private static Logger logger = LoggerFactory.getLogger(TableAction.class);
	
	@Inject
	private BasicMapper basicMapper;
	
	private String basePackage;
	
	private String dialect;
	
	

	public String getBasePackage() {
		return basePackage;
	}


	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}


	public String getDialect() {
		return dialect;
	}


	public void setDialect(String dialect) {
		this.dialect = dialect;
	}
	
	private Map<String, List<FormMap>> dbTables = new HashMap<>();
	
	protected List<FormMap> getDBTables(String databale){
		if(dbTables.get(databale)==null){
			Map<String,String> formmap = new HashMap<>();
			formmap.put("dialect", dialect);
			formmap.put("database_name", databale);
			dbTables.put(databale, basicMapper.initTableField(formmap));
		}
		return dbTables.get(databale);
	}
	
	/**
	 * 初始化数据库表字段到缓存
	 */
	@PostConstruct
	public void initTableField() {
		// 记录总记录数
		try {
			List<String> classNames = ClassUtil.scanClasses(basePackage);//true包含子目录
			if (classNames != null) {
				for (String className : classNames) {
					logger.info(" 加载 "+className);
					Class<?> clazz = Class.forName(className);
					boolean flag = clazz.isAnnotationPresent(TableSeg.class); // 某个类是不是存在TableSeg注解
					if (flag) {
						Map<String, Object> entities = (Map<String, Object>) EhcacheFactory.get(CacheClassify.ENTITY);
						if(entities == null){
							entities = new HashMap<>();
						}
						entities.put(clazz.getSimpleName(), clazz);
						EhcacheFactory.put(CacheClassify.ENTITY, entities);
						TableSeg table = (TableSeg) clazz.getAnnotation(TableSeg.class);
						Map<String, Object> tables = (Map<String, Object>) EhcacheFactory.get(CacheClassify.TABLE);
						if(tables == null){
							tables = new HashMap<>();
						}
						Map<String, Object> tableMap = (Map<String, Object>) tables.get(new String[]{table.database(),table.tableName()});
						if(tableMap == null){
							tableMap = new HashMap<>();
							tableMap.put("column_key", table.id());
							List<FormMap> f = getDBTables(table.database());
							for(int i=0;i<f.size();i++){
								if(f.get(i).getStr("TABLE_NAME").equalsIgnoreCase(table.tableName())){
									tableMap.put("fields", f.get(i).get("COLUMN_NAMES"));
									tables.put(StringUtil.isNull(table.database())?table.tableName():table.database()+"."+table.tableName(),tableMap);
									EhcacheFactory.put(CacheClassify.TABLE, tables);
									break;
								}
							}
							
						}
					} 
				}
			}
		} catch (Exception e) {
			logger.error(" 初始化数据失败,没法加载表字段到缓存 -->> "+e.fillInStackTrace());
			e.printStackTrace();
		} 
	}
	public static void main(String[] args) {
		System.out.println(System.class.getSimpleName());
		
	}
	
}
